<?php
/*
 *   This file is part of Verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */ 

class project extends entity {

	var $id;
	var $name;
	var $path;
	var $status;
	var $parent;
	var $ready;
	var $statsupdated;
	var $upd;
	
	public function __construct()
   {
   }

	// THIS CREATE A NEW INSTANCE OF THIS CLASS
	public function newInstance() {
		//$class = new ReflectionClass('user');	
		//return $class->newInstance();
		return new project;
	}

	// COMMON PUBLIC FUNCTIONS // NEEDED FOR LOADER
	public function get() {
		$sql = "SELECT * FROM ".PROJECTS." WHERE id = ".util::str($this->id);
		return $sql;
	}

	public function getParent() {
		$sql = "SELECT * FROM ".PROJECTS." WHERE id = (SELECT parent FROM ".PROJECTS." WHERE id = ".util::str($this->id).")";
		return $sql;
	}
	
	public function listAll() {
		$sql = "SELECT * FROM ".PROJECTS." WHERE parent = 0 ORDER BY name";
		return $sql;
	}

	public function listSubProjects() {
		$sql = "SELECT * FROM ".PROJECTS." WHERE parent = ".util::str($this->id)." ORDER BY name";
		return $sql;
	}
	
	public function listByStatus($upd = false) {
		$sql = "SELECT * FROM ".PROJECTS." WHERE parent = 0 AND status = ".util::str($this->status);
		if ($upd) {
			$sql .= " AND upd = true";
		}
		$sql .= " ORDER BY name";
		return $sql;
	}
	
	public function listByAny() {
		$sql = "SELECT * FROM ".PROJECTS;
		$filtered = "";
		if ($this->status != null) {
			$filtered .= " status = ".util::str($this->status);
		}
		if ($this->parent != null) {
			if ($filtered != "") $filtered .= " AND ";			
			$filtered .= " parent = ".util::str($this->parent);
		}
		if ($this->upd!=null) {
			if ($filtered != "") $filtered .= " AND ";			
			$filtered .= " upd = ".$this->upd;			
		}
		if ($filtered != "") $sql .= " WHERE " .$filtered;
		$sql .= " ORDER BY name";
		return $sql;
	}
	
	public function insert() {
		if (!isset($this->upd)) $this->upd="true";
		$sql = "INSERT INTO ".PROJECTS." (name,status,path,parent,ready,upd) ";
		$sql .= "VALUES (".util::str($this->name).",".util::str(STATUS_ACTIVE);
		$sql .= ",".util::str($this->path).",".util::str($this->parent).",".util::str(FILE_STATUS_TODO).",".$this->upd.")";
		return $sql;
	}

	public function update() {
		$sql = "UPDATE ".PROJECTS;
		$sql .= " set name = ".util::str($this->name);
		$sql .= ", status = ".util::str($this->status);
		$sql .= ", path = ".util::str($this->path);
		$sql .= ", ready = ".util::str($this->ready);
		$sql .= ", upd = ".$this->upd;
		$sql .= " WHERE id = ".util::str($this->id);
		return $sql;
	}

	public function stats() {
		$sql = "UPDATE ".PROJECTS;
		$sql .= " set statsupdated = ".util::str($this->statsupdated);
		$sql .= " WHERE id = ".util::str($this->id);
		return $sql;
	}

	public function delete() {
		$sql = "DELETE FROM ".PROJECTS." where id = ".util::str($this->id);
		return $sql;
	}
	
	public function fill($array) {
		$this->id=$array['id'];
		$this->name=$array['name'];
		$this->status=$array['status'];
		$this->path=$array['path'];
		$this->parent=$array['parent'];
		$this->ready=$array['ready'];
		$this->upd=$array['upd'];
		if ($array['statsupdated']=="0000-00-00 00:00:00") {
			$this->statsupdated="";
		} else {
			$this->statsupdated=$array['statsupdated'];	
		}
	}

}

?>